﻿using Engine;
using Engine.Graphics;
using Game;
namespace Mekiasm
{
    public class EmptyGasCell : ItemFlat, Interface.IChargeAbleItem
    {
        public static int Index;
        private Texture2D Empty = ContentManager.Get<Texture2D>("Mekiasm/Textures/Cell/air_cell");
        private Texture2D NotEmpty = ContentManager.Get<Texture2D>("Mekiasm/Textures/Cell/biogas_cell");

        public EmptyGasCell() : base(string.Empty, null)
        {
        }

        public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
        {
            long d = GetCurrentAmount(value);
            if (d == 0) return "空的气体单元";
            else
            {
                int v = GetChargeFuelValue(value);
                string n = BlocksManager.Blocks[Terrain.ExtractContents(v)].GetDisplayName(null, v);
                return $"气体单元-{n}";
            }
        }

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Gas;

        public long MaxAmount => GetMaxPower();

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            long d = GetCurrentAmount(value);
            BlocksManager.DrawFlatBlock(primitivesRenderer, value, size, ref matrix, d == 0 ? Empty : NotEmpty, color, false, environmentData);
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            return false;
        }
        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return -1f;
            else return GetCurrentAmount(value) / (float)GetMaxPower();
        }

        public override int GetMaxPower()
        {
            return 1024;
        }

        public bool CanAcceptFuel(int value)
        {
            return true;
        }

        public long GetCurrentAmount(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelAmount;
        }

        public int SetCurrentAmount(int value, int fuelValue, long varAmount)
        {
            return SaveItemSystem.ChangePower(value, varAmount, fuelValue);
        }

        public int GetChargeFuelValue(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null)
            {
                return 0;
            }
            else return d.fuelValue;
        }

    }
}
